package com.mxpio.erp.hr.controller;

import com.mxpio.erp.common.hr.entity.EmployeeSalary;
import com.mxpio.erp.common.hr.service.EmployeeSalaryService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "EmployeeSalaryController", description = "员工薪资模块")
@RestController
@RequestMapping("/erp/hr/employee/salary/")
public class EmployeeSalaryController {

    @Autowired
    private EmployeeSalaryService employeeSalaryService;

    @GetMapping("list")
    @Operation(summary = "员工薪资列表", description = "获取员工薪资列表", method = "GET")
    public Result<List<EmployeeSalary>> list(Criteria criteria) throws UnsupportedEncodingException {
        List<EmployeeSalary> employeeSalaries = employeeSalaryService.list(EmployeeSalary.class, criteria);
        return Result.OK(employeeSalaries);
    }

    @GetMapping("page")
    @Operation(summary = "员工薪资列表", description = "获取员工薪资列表", method = "GET")
    public Result<Page<EmployeeSalary>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
        Page<EmployeeSalary> page = employeeSalaryService.listPage(EmployeeSalary.class, pageAble, criteria);
        return Result.OK(page);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取员工薪资", description = "根据ID获取员工薪资", method = "GET")
    public Result<EmployeeSalary> getById(@PathVariable(name = "id", required = true) String id) {
        EmployeeSalary employeeSalary = employeeSalaryService.getById(EmployeeSalary.class, id);
        return Result.OK(employeeSalary);
    }

    @PostMapping("add")
    @Operation(summary = "新增员工薪资", description = "新增员工薪资", method = "POST")
    public Result<EmployeeSalary> add(@RequestBody EmployeeSalary employeeSalary) {
        employeeSalaryService.save(employeeSalary);
        return Result.OK(employeeSalary);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑员工薪资", description = "编辑员工薪资（全量）", method = "PUT")
    public Result<EmployeeSalary> edit(@RequestBody EmployeeSalary employeeSalary) {
        employeeSalaryService.update(employeeSalary);
        return Result.OK(employeeSalary);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "删除", description = "删除", method = "DELETE")
    public Result<EmployeeSalary> remove(@PathVariable(name = "ids", required = true) String ids) {
        String id[] = ids.split(",");
        for(String key : id){
            employeeSalaryService.delete(EmployeeSalary.class, key);
        }
        return Result.OK();
    }
    @GetMapping("calculate/{yearMonth}")
    @Operation(summary = "测算员工月度薪资", description = "测算员工月度薪资", method = "GET")
    public Result<?> calculateSummary(@Parameter(description = "yearMonth，格式2025-01",  required = true) @PathVariable(name = "yearMonth", required = true)  String yearMonth) {
        try{
            employeeSalaryService.calculateSalary(yearMonth);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.OK();
    }

    @PutMapping("finish/{yearMonth}")
    @Operation(summary = "finishSalary", description = "结账员工月度薪资", method = "PUT")
    public Result<?> finishSalary(@Parameter(description = "yearMonth，格式2025-01",  required = true) @PathVariable(name = "yearMonth", required = true)  String yearMonth) {
        try {
            employeeSalaryService.finishSalary(yearMonth);
            return Result.OK();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
